<template>
  <el-checkbox-group v-model="checkList" @change="change">
    <slot></slot>
  </el-checkbox-group>
</template>
<script>
export default {
  name: 'QPCheckboxGroup',
  props: {
    rowData: {
      type: Object,
      default: () => {
        return {}
      }
    },
    max: {
      type: Number,
      default: 4
    }
  },
  data() {
    return {
      checkList: []
    }
  },
  mounted() {
    // 回显
    console.log('this.rowData', this.rowData)
    setTimeout(() => {
      if (this.rowData.chooseOptions) {
        if (this.rowData.chooseOptions.length > 1) {
          this.checkList = this.rowData.chooseOptions.split(',')
        } else {
          this.checkList.push(this.rowData.chooseOptions)
        }
      }
    })
  },
  methods: {
    change(val) {
      // 获取当前题目的所有选项
      const optionList = JSON.parse(this.rowData.options)
      let desArrList = optionList.filter(item => {
        return val.indexOf(item.option) >= 0
      })
      desArrList = desArrList.map(item => {
        return item.des
      })
      let param = {
        contentId: this.rowData.id,
        titleType: this.rowData.titleType,
        options: this.checkList.toString(),
        optionsDesc: desArrList.toString()
      }
      this.$emit('checkChange', param)
    }
  }
}
</script>
